package com.keyroy.game.puzzle_rpg.views.tables;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.wb.swt.SWTResourceManager;

import com.keyroy.game.puzzle_rpg.dialogs.ParamEditor;
import com.keyroy.game.puzzle_rpg.models.attribute.Cost;
import com.keyroy.game.puzzle_rpg.models.attribute.Param;
import com.keyroy.game.puzzle_rpg.models.interfaces.EventListener;
import com.keyroy.game.puzzle_rpg.utils.UIUtil;

public class CostTableView extends Composite {
	private Table table;
	private Cost cost;
	private Spinner cost_gold;

	public CostTableView(Composite parent) {
		super(parent, SWT.NONE);
		setLayout(new FormLayout());

		table = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
		FormData fd_table = new FormData();
		fd_table.top = new FormAttachment(0);
		fd_table.left = new FormAttachment(0);
		fd_table.right = new FormAttachment(100);
		table.setLayoutData(fd_table);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(100);
		tblclmnNewColumn.setText("\u540D\u79F0");

		TableColumn tblclmnNewColumn_1 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_1.setWidth(100);
		tblclmnNewColumn_1.setText("\u5C5E\u6027");

		Label lblNewLabel = new Label(this, SWT.NONE);
		FormData fd_lblNewLabel = new FormData();
		lblNewLabel.setLayoutData(fd_lblNewLabel);
		lblNewLabel.setText("\u91D1\u5E01 : ");

		cost_gold = new Spinner(this, SWT.BORDER);
		fd_table.bottom = new FormAttachment(cost_gold, -6);
		fd_lblNewLabel.top = new FormAttachment(cost_gold, 3, SWT.TOP);
		fd_lblNewLabel.right = new FormAttachment(cost_gold, -6);
		cost_gold.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				cost.setGold(cost_gold.getSelection());
			}
		});
		cost_gold.setMaximum(999999);
		FormData fd_spinner = new FormData();
		fd_spinner.right = new FormAttachment(100, -10);
		cost_gold.setLayoutData(fd_spinner);

		ToolBar toolBar = new ToolBar(this, SWT.FLAT | SWT.RIGHT);
		fd_spinner.bottom = new FormAttachment(toolBar, 0, SWT.BOTTOM);
		FormData fd_toolBar = new FormData();
		fd_toolBar.bottom = new FormAttachment(100, -10);
		fd_toolBar.left = new FormAttachment(0, 10);
		toolBar.setLayoutData(fd_toolBar);

		ToolItem tltmNewItem = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				EventListener<Param> listener = new EventListener<Param>() {

					@Override
					public void onEvent(Param t) {
						cost.set(t);
						reset();
					}
				};
				UIUtil.open(new ParamEditor(getDisplay(), false, listener));
			}
		});
		tltmNewItem.setImage(SWTResourceManager.getImage(CostTableView.class, "/res/add_obj.gif"));
		tltmNewItem.setText("\u6DFB\u52A0");

		ToolItem tltmNewItem_1 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (table.getSelectionIndex() != -1) {
					cost.remove(table.getSelectionIndex());
					reset();
				}
			}
		});
		tltmNewItem_1.setImage(SWTResourceManager.getImage(CostTableView.class, "/res/remove_correction.gif"));
		tltmNewItem_1.setText("\u5220\u9664");

		ToolItem tltmNewItem_2 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {

			}
		});
		tltmNewItem_2.setImage(SWTResourceManager.getImage(CostTableView.class, "/res/debugt_obj.gif"));
		tltmNewItem_2.setText("\u8BBE\u7F6E");

		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				if (table.getSelectionIndex() != -1) {
					int index = table.getSelectionIndex();
					if (index != -1) {
						EventListener<Param> listener = new EventListener<Param>() {

							@Override
							public void onEvent(Param t) {
								cost.set(t);
								reset();
							}
						};
						UIUtil.open(new ParamEditor(getDisplay(), cost.get(index), false, listener));
					}
				}
			}
		});

		set(new Cost());
	}

	public void set(Cost cost) {
		if (cost != null) {
			this.cost = cost;
			reset();
		}
	}

	public Cost getCost() {
		return cost;
	}

	public void reset() {
		table.clearAll();
		if (cost != null) {
			for (int i = 0; i < cost.params.size(); i++) {
				Param param = cost.params.get(i);
				TableItem tableItem = new TableItem(table, SWT.NONE);
				tableItem.setText(param.getString());
			}
			cost_gold.setSelection(cost.gold);
		}
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
